package com.cyou.qb.admin.web;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestMapping;

import com.cyou.nad.datastore.query.JdbcPage;
import com.cyou.qb.common.domain.BlackList;
import com.cyou.qb.common.service.BlackListService;

/**
 * <p>后台黑名单管理</p>
 * @author zoudongchao
 *
 */
@Controller
@RequestMapping(value = "/blacklist")
public class BlackListController extends BaseController {
	@Resource(name = "BlackListServiceCacheImpl")
	private BlackListService blackListService;

	/**
	 * <p>添加黑名单</p>
	 * @param request
	 * @param response
	 */
	@SuppressWarnings("deprecation")
	@RequestMapping(value = "/add")
	public void ajaxAddBlackList(HttpServletRequest request, HttpServletResponse response) throws Exception{
		//获取参数
		String passport = StringUtils.trimToEmpty(request.getParameter("passport"));
		String forbidden = StringUtils.trimToEmpty(request.getParameter("forbid"));
		String memo = StringUtils.trimToEmpty(request.getParameter("memo"));
		String[] forbiddenTime = forbidden.split("_");//应当是两段, 1,1 表示封号一年
		
		//非法参数
		if ("".equals(passport) || passport.length()>50 || "".equals(forbidden) || "".equals(memo) || forbiddenTime.length != 2) {
			log.debug("[ajaxAddBlackList]参数非法");
			print(response, "error");
			return;
		}

		try {
			Date now = new Date();
			Date end = DateUtils.add(now, Integer.parseInt(forbiddenTime[1]), Integer.parseInt(forbiddenTime[0]));
			
			//查询，是否已存在
			List<BlackList> blackList = blackListService.findBlackList(passport, now);//不排除对一个账号添加多个黑名单的可能
			if (blackList != null && blackList.size() != 0) {
				//返回错误信息 已经有记录
				log.info("[add]记录已存在");
				print(response, "existed");
				return;
			}
			//插入
			int result = blackListService.saveBlackList(0, passport, "", now, end, forbidden, memo);
			if (result <= 0) {
				//返回插入失败
				log.info("[add]插入数据库失败");
				print(response, "error");
				return;
			}
			//插入成功
			log.debug("黑名单插入成功");
			print(response,"success");
			return;
		} catch (Exception e) {
			log.error("[BlackListController][ajaxAddBlackList]数据库操作异常",e);
			print(response, "error");
		}
	}

	@RequestMapping(value = "/remove")
	public void ajaxDeleteBlackList(Model model, HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		try {
			//获取参数
			String blackListId = StringUtils.trimToEmpty(request.getParameter("blacklistId"));
			//参数判断
			Assert.hasText(blackListId, "删除黑名单失败");

			//删除
			blackListService.removeBlackList(Integer.parseInt(blackListId));

			//删除成功
			print(response, "success");
		} catch (Exception e) {
			log.error("[BlackListController][ajaxDeleteBlackList]删除黑名单失败", e);
			print(response, "error:" + e.getMessage());
		}
	}

	/**
	 * <p>进入黑名单管理页</p>
	 * @param model
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/index")
	public String index(Model model, HttpServletRequest request, HttpServletResponse response) {
		//获取查询参数
		String passport = StringUtils.trimToEmpty(request.getParameter("s_passport"));
		String nickname = StringUtils.trimToEmpty(request.getParameter("s_nickname"));
		
		//查询
		JdbcPage page = blackListService.findPageQueryBlackList(passport, nickname, getPageNo(),
				getPageSize(DEFAULT_PAGE_SIZE));

		//封装
		saveAllParameters(request);//存储相关查询参数
		model.addAttribute("page", page);
		
		return "activity/blacklist";
	}

	public BlackListService getBlackListService() {
		return blackListService;
	}

	public void setBlackListService(BlackListService blackListService) {
		this.blackListService = blackListService;
	}
}
